from django.db import models
from utils.models import BaseModel
from utils.utils import make_order_number


class Order(BaseModel):
    """订单模型"""
    PAY_METHOD_CHICES = (
        (1, '货到付款'),
        (2, '微信支付'),
        (3, '支付宝'),
        (4, '银联支付')
    )
    ORDER_STATUS_CHOICES = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成')
    )
    order_number = models.CharField('订单号', primary_key=True, unique=True, max_length=40, default=make_order_number())
    trade_no = models.CharField('支付编号', max_length=128)  # 支付宝支付完成后,会返回一个支付编号
    status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name='状态')
    pay_method = models.SmallIntegerField(choices=PAY_METHOD_CHICES, default=3, verbose_name='支付方式')
    address = models.ForeignKey('users.Address', models.CASCADE, verbose_name='地址')
    user = models.ForeignKey('users.UserInfo', models.CASCADE, verbose_name='用户')
    count_price = models.DecimalField('总价', max_digits=9, decimal_places=2)

    def __str__(self):
        return self.user

    class Meta:
        verbose_name_plural = '订单管理'
        verbose_name = verbose_name_plural


class OrderItem(BaseModel):
    goods = models.ForeignKey('goods.Goods', models.CASCADE, verbose_name='商品')
    color = models.ForeignKey('goods.Color', models.CASCADE, verbose_name='颜色')
    size = models.ForeignKey('goods.Size', models.CASCADE, verbose_name='尺寸')
    count = models.PositiveIntegerField(verbose_name='数量')
    order = models.ForeignKey(Order, models.CASCADE, verbose_name='订单')

    def __str__(self):
        return self.goods.g_name

    class Meta:
        verbose_name_plural = '单个商品订单'
        verbose_name = verbose_name_plural

